rm(list = ls())

library(stargazer)
library(haven)
library(readstata13)
library(Hmisc)
library(foreign)
require(ggplot2)
require(MASS)
library(reshape)
library(interplot)
library(gdata)
install.packages("cem")
library(cem)
library(reshape2)
require(Matching)
library(data.table)
install.packages("ggeasy")
library(ggeasy)
library(dplyr)
install.packages("janitor")
library(janitor)

#########################################################################
# Replication Code for: "Personal Networks, State Financial Backing and Foreign Direct Investment"                
# Seungjun Kim
# Published in Comparative Political Studies
# Used R studio Version 1.2.1335
#########################################################################
test1 <- read.dta("~/Downloads/personalnetworks_cps_dataverse(final).dta")
test1 <- rename(test1,
                Board_Ties_to_State_Banks_Label="finconlabel",
                Bank_Loan_Amount="lamount_final",
                FDIrelated_Bank_Loan_Amount="lamount_final_fdirelated",
                FDIProjects_Amount="lpostamountuntil2016",
                FDIProjects_Number="lpostnumberuntil2016",
                Board_Ties_to_State_Banks="fincondum",
                Firm_Size="wsmarketcap11",
                Debt_Ratio="debtratio2011",
                Profitability="roe2011",
                Chaebol="chaebol",
                Board_Ties_to_Incoming_President="parkrelated",
                Board_Ties_to_Tax_Authority="taxdum",
                Return_on_Investment= "roidiff",
                Income_Tax_Rates="lincometax")

#################################################
# Figure 1: Loan Distribution (among large firms)
#################################################
Large_Firm = subset(test1, logfirmsize>5.2)
Large_Firm$Board_Ties_to_State_Banks_Label <- ordered(Large_Firm$Board_Ties_to_State_Banks_Label, levels=(c("not connected", "connected")))
test1$Board_Ties_to_State_Banks_Label <- ordered(test1$Board_Ties_to_State_Banks_Label, levels=(c("not connected", "connected")))

#All Loans
Mean = Large_Firm %>%
  group_by(Board_Ties_to_State_Banks_Label) %>%
  summarise(avg_AB = mean(Bank_Loan_Amount))
p1 = ggplot(Mean, aes(x = Board_Ties_to_State_Banks_Label,
                      y = avg_AB, fill=Board_Ties_to_State_Banks_Label))
print(p1)

plotA = p1 + 
  geom_bar(stat = "identity")+scale_fill_manual(values = c("grey75", "grey25"))

plotA+
  labs(title = "All Loans",
       x = "Financial Board Connections",
       y = "Loan Volume (logged)")+ ggeasy::easy_center_title()+  theme(legend.position="none")

#FDI-related Loans
Mean_fdi = Large_Firm %>%
  group_by(Board_Ties_to_State_Banks_Label) %>%
  summarise(avg_AB = mean(FDIrelated_Bank_Loan_Amount))

p2 = ggplot(Mean_fdi, aes(x = Board_Ties_to_State_Banks_Label,
                          y = avg_AB, fill=Board_Ties_to_State_Banks_Label))
print(p2)

plotB = p2 + 
  geom_bar(stat = "identity")+scale_fill_manual(values = c("grey75", "grey25"))

plotB+
  labs(title = "FDI-related Loans",
       x = "Financial Board Connections",
       y = "Loan Volume (logged)")+ ggeasy::easy_center_title()+  theme(legend.position="none")


###########################################################
# Table 1: OLS regression analyses to explain FDI decisions
###########################################################
summary(m1<- lm(FDIProjects_Amount  ~ Board_Ties_to_State_Banks + factor(sic_category) , data=test1))
summary(m2<- lm(FDIProjects_Amount  ~ Firm_Size + factor(sic_category) , data=test1))
summary(m3<- lm(FDIProjects_Amount  ~ Board_Ties_to_State_Banks + Firm_Size+  factor(sic_category) , data=test1))
summary(m4<- lm(FDIProjects_Amount  ~ Board_Ties_to_State_Banks +Firm_Size+ Debt_Ratio + Profitability + Chaebol+ factor(sic_category), data=test1))
summary(m5<- lm(FDIProjects_Amount  ~ Board_Ties_to_State_Banks*Firm_Size + Debt_Ratio + Profitability + Chaebol+ factor(sic_category), data=test1))

t1<-stargazer(m1, m2, m3, m4, m5)

##########################################################################
#Figure 2: Marginal Effects of Financial Board Ties on FDI Project Amounts
##########################################################################
interplot(m = m5, var1 = "Board_Ties_to_State_Banks", var2 = "Firm_Size",ci= .95, esize=1.5)+ theme_bw()  +
  xlab("Size of Firms (in billion US Dollars)") +
  ylab("FDI Project Amounts (%) ") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  ggtitle("")+theme(plot.title = element_text(hjust = 0.5))+theme(plot.title = element_text(face="bold"))

#################################################################################################################
# Table 2: OLS regression analyses to explain FDI decisions using matched samples and low-risk sector investments
#################################################################################################################
## Subsetting
vars1 <- c("Board_Ties_to_State_Banks", "FDIProjects_Amount", "Firm_Size", "Debt_Ratio", "Profitability", "Chaebol", "sic_category")
matchsub <- test1[vars1]

# Setting up cutpoints. These intervals were chosen to create aobut even distribution of observations in the category.
quantile(test1$Firm_Size, na.rm=TRUE)
quantile(test1$Debt_Ratio, na.rm=TRUE)
quantile(test1$Profitability, na.rm=TRUE)

sizecut<-c(4.134583e-04, 4.883938e-03, 1.223186e-02, 5.022811e-02, 1.379492e+01)  
debtcut<-c(0, 0.09555332, 0.2421672, 0.40076075, 0.84410536) 
roecut<-c(-1307.74, 0.25, 6.32, 12.20, 441.23)

# Coarsening using cutpoints
coarsencut <-cem(treatment= "Board_Ties_to_State_Banks", data=matchsub, drop=c("FDIProjects_Amount", "sic_category"), cutpoints=list(Firm_Size=sizecut, Debt_Ratio=debtcut, Profitability=roecut))
coarsencut$breaks

# Retrieving weights
coarsenweight <-coarsencut$w

# Estimating the Causal Effect from CEM Output
summary(weightedmatching2 <-glm(FDIProjects_Amount ~  Board_Ties_to_State_Banks*Firm_Size + Debt_Ratio + Profitability + Chaebol + factor(sic_category)+ factor(sic_category), weights=coarsenweight, data=matchsub, family = "gaussian"))

# Using low risk industries to alleviate concerns about reverse causality
summary(lowrisk2<- lm(lamountrobust  ~ Board_Ties_to_State_Banks*Firm_Size + Debt_Ratio + Profitability + Chaebol + factor(sic_category), data=test1))

stargazer(weightedmatching2, lowrisk2)

##########################################################################
#Figure 2: Marginal Effects of Financial Board Ties on FDI Project Amounts
##########################################################################
matchingf<-interplot(m = weightedmatching2, var1 = "Board_Ties_to_State_Banks", var2 = "Firm_Size",ci= .95, esize=1.5)+ theme_bw()  +
  xlab("Size of Firms (in billion US Dollars)") +
  ylab("FDI Project Amounts (%) ") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  ggtitle("CEM Matched Samples")+theme(plot.title = element_text(hjust = 0.5))+theme(plot.title = element_text(face="bold"))

lowriskf<-interplot(m = lowrisk2, var1 = "Board_Ties_to_State_Banks", var2 = "Firm_Size",ci= .95, esize=1.5)+ theme_bw()  +
  xlab("Size of Firms (in billion US Dollars)") +
  ylab("FDI Project Amounts (%) ") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  ggtitle("Low-Risk Sector Investments")+theme(plot.title = element_text(hjust = 0.5))+theme(plot.title = element_text(face="bold"))

par(mfrow=c(1,2))
devtools::install_github("thomasp85/patchwork")
library(patchwork)
matchingf + lowriskf

##########################################################################################################
# Table 3: OLS regression analyses to explain FDI decisions using different types of political connections
##########################################################################################################
summary(m1p<- lm(FDIProjects_Amount  ~ Board_Ties_to_Incoming_President+ factor(sic_category), data=test1))
summary(m2p<- lm(FDIProjects_Amount  ~ Board_Ties_to_Incoming_President+ Firm_Size + Debt_Ratio + Profitability + Chaebol  + factor(sic_category), data=test1))
summary(m3p<- lm(FDIProjects_Amount  ~ Board_Ties_to_Incoming_President* Firm_Size + Debt_Ratio + Profitability + Chaebol  + factor(sic_category), data=test1))

summary(m4p<- lm(FDIProjects_Amount  ~ Board_Ties_to_Tax_Authority+ factor(sic_category), data=test1))
summary(m5p<- lm(FDIProjects_Amount  ~ Board_Ties_to_Tax_Authority+ Firm_Size + Debt_Ratio + Profitability + Chaebol  + factor(sic_category), data=test1))
summary(m6p<- lm(FDIProjects_Amount  ~ Board_Ties_to_Tax_Authority* Firm_Size + Debt_Ratio + Profitability + Chaebol  + factor(sic_category), data=test1))

stargazer(m1p, m2p, m3p, m4p, m5p, m6p)

#########
#Appendix
#########

#############################
#Table A1: Summary Statistics
#############################
summary <- test1[1:10]
stargazer(summary)

#############################
#Table A2: Distribution of Firms with Financial Board Connections by Industry
#############################
FinancialConnection<- test1 %>%
  filter(Board_Ties_to_State_Banks == 1)

ta2 <- FinancialConnection %>%
  tabyl(sic_category)

ta2 %>%
  adorn_totals("row") %>%
  adorn_pct_formatting()

#############################
#Table A3: Distribution of Firms with President Park 
#############################
TaxConnection<- test1 %>%
  filter(Board_Ties_to_Incoming_President == 1)

ta3 <- TaxConnection %>%
  tabyl(sic_category)

ta3 %>%
  adorn_totals("row") %>%
  adorn_pct_formatting()

#############################
#Table A4: Distribution of Firms with Tax Authority 
#############################
ParkConnection<- test1 %>%
  filter(Board_Ties_to_Tax_Authority == 1)

ta4 <- ParkConnection %>%
  tabyl(sic_category)

ta4 %>%
  adorn_totals("row") %>%
  adorn_pct_formatting()

#############################
#Figure A1: Bank Loan Distribution (all firms)
#############################
#All loans
Mean = test1%>%
  group_by(Board_Ties_to_State_Banks_Label) %>%
  summarise(avg_AB = mean(Bank_Loan_Amount))

p3 = ggplot(Mean, aes(x = Board_Ties_to_State_Banks_Label,
                      y = avg_AB, fill=Board_Ties_to_State_Banks_Label))
print(p3)

plotC = p3 + 
  geom_bar(stat = "identity")+scale_fill_manual(values = c("grey75", "grey25"))

plotC+
  labs(title = "All Loans",
       x = "Financial Board Connections",
       y = "Loan Volume (logged)")+ ggeasy::easy_center_title()+theme(legend.position="none")

#FDI-related
Mean_fdi = test1 %>%
  group_by(Board_Ties_to_State_Banks_Label) %>%
  summarise(avg_AB = mean(FDIrelated_Bank_Loan_Amount))

p4 = ggplot(Mean_fdi, aes(x = Board_Ties_to_State_Banks_Label,
                          y = avg_AB, fill=Board_Ties_to_State_Banks_Label))
print(p4)

plotD = p4 + 
  geom_bar(stat = "identity")+scale_fill_manual(values = c("grey75", "grey25"))

plotD+
  labs(title = "FDI-related Loans",
       x = "Financial Board Connections",
       y = "Loan Volume (logged)")+ ggeasy::easy_center_title()+theme(legend.position="none")

#############################
#Table A5: OLS regression analyses to explain FDI decisions (with logged firm size)
#############################
summary(m1a5<- lm(FDIProjects_Amount  ~ Board_Ties_to_State_Banks + factor(sic_category) , data=test1))
summary(m2a5<- lm(FDIProjects_Amount  ~ logfirmsize + factor(sic_category), data=test1))
summary(m3a5<- lm(FDIProjects_Amount  ~ Board_Ties_to_State_Banks +logfirmsize+ Debt_Ratio + Profitability + Chaebol+ factor(sic_category), data=test1))
summary(m4a5<- lm(FDIProjects_Amount  ~ Board_Ties_to_State_Banks*logfirmsize + Debt_Ratio + Profitability + Chaebol+ factor(sic_category), data=test1))

stargazer(m1a5, m2a5, m3a5, m4a5)

#############################
#Table A6: Robustness Check: OLS regression analyses to explain FDI decisions (FDI project numbers)
#############################
summary(m1a6<- lm(FDIProjects_Number  ~ Board_Ties_to_State_Banks + factor(sic_category) , data=test1))
summary(m2a6<- lm(FDIProjects_Number  ~ Firm_Size + factor(sic_category), data=test1))
summary(m3a6<- lm(FDIProjects_Number  ~ Board_Ties_to_State_Banks + Firm_Size+  factor(sic_category) , data=test1))
summary(m4a6<- lm(FDIProjects_Number  ~ Board_Ties_to_State_Banks +Firm_Size+ Debt_Ratio + Profitability + Chaebol+ factor(sic_category), data=test1))
summary(m5a6<- lm(FDIProjects_Number  ~ Board_Ties_to_State_Banks*Firm_Size + Debt_Ratio + Profitability + Chaebol+ factor(sic_category), data=test1))

stargazer(m1a6, m2a6, m3a6, m4a6, m5a6)


#############################
#Table A7: OLS regression analyses to explain the Return on Investment
#############################
summary(m1a7<- lm(Return_on_Investment  ~ Board_Ties_to_State_Banks, data=test1))
summary(m2a7<- lm(Return_on_Investment  ~ Board_Ties_to_State_Banks +Firm_Size+ Debt_Ratio + Profitability + Chaebol+ factor(sic_category), data=test1))
summary(m3a7<- lm(Return_on_Investment  ~ Board_Ties_to_State_Banks*Firm_Size + Debt_Ratio + Profitability + Chaebol+ factor(sic_category), data=test1))

stargazer(m1a7, m2a7, m3a7)


#############################
#Table A8: OLS regression analyses to explain Income Tax Rates
#############################
summary(m1a8<- lm(Income_Tax_Rates  ~ Board_Ties_to_State_Banks, data=test1))
summary(m2a8<- lm(Income_Tax_Rates  ~ Board_Ties_to_State_Banks +Firm_Size+ Debt_Ratio + Profitability + Chaebol+ factor(sic_category), data=test1))
summary(m3a8<- lm(Income_Tax_Rates  ~ Board_Ties_to_State_Banks*Firm_Size + Debt_Ratio + Profitability + Chaebol+ factor(sic_category), data=test1))

stargazer(m1a8, m2a8, m3a8)


